//
//  GameViewController.swift
//  DouYuZB
//
//  Created by DSDL on 2016/11/3.
//  Copyright © 2016年 鼎商动力. All rights reserved.
//

import UIKit

private let kEdgemargin: CGFloat = 10
private let kItemW: CGFloat = (kScreenW - 2*kEdgemargin)/3
private let kItemH: CGFloat = kItemW * 6 / 5

private let kHeadViewH: CGFloat = 50
private let kGameViewH: CGFloat = 90

private let kHeadViewID: String = "kHeadViewID"
class GameViewController: BaseViewController {
    //MARK: - 懒加载属性
    
    fileprivate lazy var gameVM = GameViewModel()
    
    fileprivate lazy var collectionView: UICollectionView = { [unowned self] in
        //1.创建布局
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kItemW, height: kItemH)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        layout.headerReferenceSize  = CGSize(width: kScreenW, height: kHeadViewH)
        //2.创建collectionView
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.register(CollectionViewGameCell.NibObject(), forCellWithReuseIdentifier: CollectionViewGameCell.identifier)
        collectionView.register( CollectionReusableView.NibObject(), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kHeadViewID)
        collectionView.backgroundColor = UIColor.white
        collectionView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        collectionView.dataSource = self
        
        return collectionView
        }()
    
    fileprivate lazy var topHeadView: CollectionReusableView = {
        let topHeadView: CollectionReusableView  = CollectionReusableView.fromNib(nibName: "CollectionReusableView")
        topHeadView.frame = CGRect(x: 0, y: -(kHeadViewH + kGameViewH), width: kScreenW, height: kHeadViewH)
        topHeadView.titleLabel.text = "常见"
        topHeadView.iconImageView.image = UIImage(named:"Img_orange")
        topHeadView.moreBtn.isHidden = true
        return topHeadView
    }()
    //游戏的view
    fileprivate lazy var gameView: RecommendGameView = {
        let gameView = RecommendGameView.fromNib(nibName: "RecommendGameView")
        gameView.frame = CGRect(x: 0, y: -kGameViewH, width: kScreenW, height: kGameViewH)
        return gameView as! RecommendGameView
    }()
    
    //MARK: - 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        //1.设置UI
        setupUI()
        //2.请求数据
        requestdata()
    }
}

//MARK: - 设置UI
extension GameViewController {
    override func setupUI(){
        //0. 给contentSView赋值
        contentsView = collectionView
        //1.添加collectionView
        view.addSubview(collectionView)
        //2.添加顶部headView
        collectionView.addSubview(topHeadView)
        //3.添加常用游戏的view到 collectionView上
        collectionView.addSubview(gameView)
        //设置collectionView的内编剧
        collectionView.contentInset = UIEdgeInsets(top: kHeadViewH + kGameViewH, left: 0, bottom: 0, right: 0)
        super.setupUI()
    }
}
//MARK: - 请求数据
extension GameViewController {
    
    //请求数据
    fileprivate func requestdata(){
        gameVM.loadAllGamesData {
            //1.展示全部游戏
            self.collectionView.reloadData()
            
            //2.展示常用游戏
            self.gameView.groupsArr = Array(self.gameVM.games[0..<10])
            //3.数据请求完成
            self.loadDataFinish()
        }
    }
}

//MARK: - UICollectionViewDataSource
extension GameViewController :UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return gameVM.games.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewGameCell.identifier, for: indexPath) as!  CollectionViewGameCell
        cell.basegame = gameVM.games[indexPath.item]
        
        
        //TODO:  - 这个留作研究,最后剩余一个或者两个cell 不让他显示下面的线的逻辑 cell
        var lineCount = gameVM.games.count % 3
        lineCount = lineCount == 0 ? 3 : lineCount
        
        if (indexPath.item + lineCount) > (gameVM.games.count - 1) {
            cell.titleLabel.textColor = UIColor.red
        }else{
            cell.titleLabel.textColor = UIColor.black
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        //1.取出section的HeadView
        let headView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kHeadViewID, for: indexPath) as! CollectionReusableView
        //2.给headView设置属性
        headView.titleLabel.text = "全部"
        headView.iconImageView.image = UIImage(named:"Img_orange")
        headView.moreBtn.isHidden = true
        return headView
    }
}
